package com.crb.smartcard.cardutils;




import com.crb.smartcard.SmartCardConfig;
import com.crb.smartcard.cardutils.entity.TradeInfo;
import com.crb.smartcard.cardutils.entity.TransactionRecordsEntity;
import com.crb.smartcard.core.bean.CardResult;
import com.crb.smartcard.core.oma.SmartCard;
import com.crb.smartcard.utils.ApduUtil;
import com.crb.smartcard.utils.LogUtil;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取交易记录
 */
public class TransactionRecordsUtil {
    private static final String TAG = "TransactionRecordsUtil";

    public static TransactionRecordsEntity getTransactionRecords(String aid) {
        String selectAid = ApduUtil.getSelectAid(aid);
        CardResult execute = SmartCard.execute(selectAid);
        LogUtil.e(TAG, "CardResult:" + execute);
        if (execute.getStatus() != 0 || !execute.getSw().equalsIgnoreCase("9000")) {
            return null;
        }
        //获取交易记录
        List<TradeInfo> tradeInfos = new ArrayList<TradeInfo>();
        for (int i = 1; i < 11; i++) {
            String capdu = "00B20" + Integer.toHexString(i) + "C417";
            LogUtil.e(TAG, "查询指令：" + capdu);
            CardResult cardResult = SmartCard.execute(capdu);
            LogUtil.e(TAG, "查询结果-CardResult:" + cardResult);
            if (cardResult.getStatus() == 0) {
                if (cardResult.getRapdu().equals("") || !cardResult.getSw().equalsIgnoreCase("9000")
                        || cardResult.getRapdu().equalsIgnoreCase("0000000000000000000000000000000000000000000000")) {
                    break;
                }
                TradeInfo tradeInfo = parseTradeRecord(cardResult.getRapdu());
                LogUtil.e(TAG, "tradeInfo:" + tradeInfo.toString());
                tradeInfos.add(tradeInfo);
            }
        }
        TransactionRecordsEntity transactionRecordsEntity = new TransactionRecordsEntity();
        transactionRecordsEntity.setCode(SmartCardConfig.SUCCESSFUL_CODE);
        transactionRecordsEntity.setInfo("获取交易记录成功");
        transactionRecordsEntity.setTradelist(tradeInfos);
//        String s = new Gson().toJson(transactionRecordsEntity);
        LogUtil.e(TAG, transactionRecordsEntity.toString());
        SmartCard.closeChannel();
        return transactionRecordsEntity;
    }

    /**
     * 解析交易记录
     */
    private static TradeInfo parseTradeRecord(String tradeRecord) {
        String tradeNo = tradeRecord.substring(0, 4);
        int tradeMoney = new BigInteger(tradeRecord.substring(10, 18), 16).intValue();
        String tradeType = tradeRecord.substring(18, 20);
        String tradeDate = tradeRecord.substring(32, 40);
        String tradeTime = tradeRecord.substring(40, 46);
        String posid = tradeRecord.substring(20, 32);

        TradeInfo tradeInfo = new TradeInfo();
        tradeInfo.setTradeNo(tradeNo);
        tradeInfo.setTradeMoney(tradeMoney);
        tradeInfo.setTradeDate(tradeDate);
        tradeInfo.setTradeTime(tradeTime);
        tradeInfo.setTradeType(tradeType);
//        tradeInfo.setTradeRecord(tradeRecord);
        tradeInfo.setPosId(posid);
       /* if ("02".equals(tradeType)) {
            tradeInfo.setTradeType(1);//充值
        } else if ("06".equals(tradeType)) {
            tradeInfo.setTradeType(0);//消费
        }*/
        return tradeInfo;
    }
}
